import config from '@/utils/config'

const AJAX = ({
	name = '',
	method = 'GET',
	data = {},
	header = false
}) => {
	let option = {
		url: config.api[name],
		method,
		header: header ? { Authorization: uni.getStorageSync('user') ? uni.getStorageSync('user').token : 0 } : {}
	}
	if (method === 'GET') {
		data._t = new Date().getTime()
		option.data = data
	} else if (method === 'POST') {
		option.data = data
	}

	return uni.request(option)
		.then(resp => {
			const [err, data] = resp
			if (data && data.data) {
				let res = data.data
				return [null, res]
			} else {
				return ['return none']
			}
		})
		.catch(function(error) {
			if (error && error.response && error.response.data) {
				return [error.response.data]
			} else {
				return ['return error']
			}
		})
}

export default AJAX
